package cn.likun.kit.stream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

import cn.likun.kit.validate.ValidateCommons;

/**
 * <p>流读写工具</p>
 *@version1.0
 */
public class StreamCommons {
	
	private StreamCommons() {}
	
	private static byte[] buffer = new byte[1024];
	/**
	 * <p>二进制方式传输流</p>
	 * @param in 输入流  finally close
	 * @param out 输出流 finally close if closeOut is true 
	 * @param closeOut
	 * @throws Exception
	 */
	public static void binaryTransport(InputStream in, OutputStream out, boolean closeOut) throws IOException {
		if (ValidateCommons.isNotEmpty(in) && ValidateCommons.isNotEmpty(out)) {
			BufferedInputStream bufIn = new BufferedInputStream(in);
			BufferedOutputStream bufOut = new BufferedOutputStream(out);
			try {
				int index = 0 ;
				while ((index = bufIn.read(buffer)) != -1) {
					bufOut.write(buffer,0,index);
					bufOut.flush();
				}
			}finally{
				if (bufIn != null) {
					bufIn.close();
				}
				if (closeOut && bufOut != null) {
					bufOut.close();
				}
				buffer = new byte[1024];
			}
		}
	}
	/**
	 * <p>二进制方式传输流</p>
	 * @param in 输入流  finally close
	 * @param out 输出流 finally close 
	 * @throws Exception
	 */
	public static void binaryTransport(InputStream in ,OutputStream out) throws IOException {
		binaryTransport(in, out, true);
	}
	/**
	 * <p>读取二进制流 , 并返回字节数组</p>
	 * @param in 输入流 , finally close
	 * @throws Exception
	 */
	public static byte[] binaryReader(InputStream in) throws Exception {
		ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream() ;
		if (ValidateCommons.isNotEmpty(in)) {
			BufferedInputStream bufIn = new BufferedInputStream(in);
			try {
				int index = 0;
				while ((index = bufIn.read(buffer)) != -1) {
					byteArrayOut.write(buffer, 0, index);
					byteArrayOut.flush();
				}
			} finally {
				if (bufIn != null) {
					bufIn.close();
				}
				if (byteArrayOut != null) {
					byteArrayOut.close();
				}
				buffer = new byte[1024];
			}
		}
		return byteArrayOut.toByteArray();
	}
	/**
	 * <p>文本式传输流</p>
	 * @param reader 输入流 finally close 
	 * @param writer 输出流 finally close if closeWriter is true
	 * @param closeWriter 
	 * @throws Exception
	 */
	public static void textTransport(Reader reader ,Writer writer,boolean closeWriter) throws IOException {
		if (ValidateCommons.isNotEmpty(writer) && ValidateCommons.isNotEmpty(reader)) {
			BufferedReader bufReader = new BufferedReader(reader);
			BufferedWriter bufWriter = new BufferedWriter(writer);
			try {
				String line = null;
				while ((line = bufReader.readLine()) != null) {
					bufWriter.write(line);
					bufWriter.flush();
					bufWriter.newLine();
				}
			} finally {
				if (bufReader != null) {
					bufReader.close();
				}
				if (closeWriter && bufWriter != null) {
					bufWriter.close();
				}
			}
		}
		
	}
	/**
	 * <p>文本式传输流</p>
	 * @param reader  输入流 finally close
	 * @param writer  输出流 finally close
	 * @throws Exception
	 */
	public static void textTransport(Reader reader , Writer writer) throws IOException {
		textTransport(reader, writer, true);
	}
	/**
	 * <p>读取文本流，并返回字符串</p>
	 * @param reader 输入流
	 * @return
	 * @throws IOException 
	 * @throws Exception
	 */
	public static String textReader(Reader reader) throws IOException  {
		StringBuffer text = new StringBuffer();
		if (ValidateCommons.isNotEmpty(reader)) {
			BufferedReader bufReader = new BufferedReader(reader);
			try {
				String line = null;
				while ((line=bufReader.readLine()) != null) {
					text.append(line).append("\n");
				}
			} finally {
				if (bufReader != null) {
					bufReader.close();
				}
			}
		}
		return text.toString();
	}
	
}
